package com.sunyur.tailor.channel.service.inner.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.contract.model.ContractCapInfoModel;
import com.sunyur.tailor.channel.service.inner.contract.param.ContractCapCreateParam;
import com.sunyur.tailor.channel.service.inner.contract.param.ContractCapUpdateParam;
import com.sunyur.tailor.channel.service.inner.contract.param.GetContractInfoParam;
import com.sunyur.tailor.channel.service.inner.contract.service.ContractCapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 16:44 2021/9/7
 */
@Slf4j
@Service(value = "contractCapService")
public class ContractCapServiceImpl implements ContractCapService {

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.contractCapUpdate}")
    private String contractCapUpdate;

    @Value("${sunyur.api.contractCapCreateBatch}")
    private String contractCapCreateBatch;

    @Value("${sunyur.api.listContractCapByContractCodes}")
    private String listContractCapByContractCodes;

    /**
     * 批量创建合同帽
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> createBatch(List<ContractCapCreateParam> param) {
        log.info("ContractCapServiceImpl createBatch param is {} ", JSON.toJSONString(param));
        SunyurResult<Boolean> sunyurResult = null;
        try {
            sunyurResult = sunyurClient.execPost(contractCapCreateBatch, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {
            });
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            log.error("ContractCapServiceImpl createBatch fail,param is {} ", JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
        }
        log.info("ContractCapServiceImpl createBatch param is {} , result is {}", JSON.toJSONString(param), JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 修改合同帽
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> update(ContractCapUpdateParam param) {
        log.info("ContractCapServiceImpl update param is {} ", JSON.toJSONString(param));
        SunyurResult<Boolean> sunyurResult = null;
        try {
            sunyurResult = sunyurClient.execPost(contractCapUpdate, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {
            });
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            log.error("ContractCapServiceImpl update fail,param is {} ", JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
        }
        log.info("ContractCapServiceImpl update param is {} , result is {}", JSON.toJSONString(param), JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 批量获取合同帽信息
     * @param param
     * @return
     */
    @Override
    public ChannelListResult<ContractCapInfoModel> listContractCapByContractCodes(GetContractInfoParam param) {
        log.info("ContractCapServiceImpl listContractCapByContractCodes param is {} ", JSON.toJSONString(param));
        SunyurListResult<ContractCapInfoModel> sunyurResult = null;
        try {
            sunyurResult = sunyurClient.execListPost(listContractCapByContractCodes, JSON.toJSONString(param), new TypeReference<SunyurListResult<ContractCapInfoModel>>() {
            });
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            log.error("ContractCapServiceImpl listContractCapByContractCodes fail,param is {} ", JSON.toJSONString(param));
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
        }
        log.info("ContractCapServiceImpl listContractCapByContractCodes param is {} , result is {}", JSON.toJSONString(param), JSON.toJSONString(sunyurResult));
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }
}
